package com.sky.controller.setmeal;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api("套餐管理")
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @ApiOperation("新增套餐")
    @PostMapping()
    @CacheEvict(cacheNames = "setmealCache",key = "setmealDTO.categoryId")
    public Result saveSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐的信息为{}",setmealDTO);
        setMealService.saveSetMeal(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> searchByPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("传入的套餐分页查询信息为{}",setmealPageQueryDTO);
        PageResult pageResult=setMealService.searchByPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("批量删除的id为{}",ids);
        setMealService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("查询菜品的id为{}",id);
        SetmealVO setmealVO=setMealService.getById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("需要修改套餐属性为{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startAndStop(@PathVariable Integer status,Long id){
        setMealService.startAndStop(status,id);
        return Result.success();
    }
}
